function OrderTable()
{
	events = arguments.callee.caller.arguments[0] || event;
	cellObj = events.target || events.srcElement;
	if (cellObj.tagName.toLocaleLowerCase() != "td")
		return ;

	colNum 	= cellObj.cellIndex;
	table 	= cellObj.parentNode.parentNode;
	if (cellObj.parentNode.rowIndex == 0)
	{
		sign = 1;
		if (cellObj.getAttribute("order")) {
			sign = parseInt(cellObj.getAttribute("order"));
			sign *= -1;
		}
		cellObj.setAttribute("order", sign);

		Arr = ReadTable(table);
		SortArray(Arr, colNum, function(a,b){
			// here to code compare function
			return sign * (parseInt(a)-parseInt(b));
		});

		WriteTable(table, Arr);
	}
}

function ReadTable(Table)
{
	Arr = new Array(Table.rows.length);
	for (i = 0; i < Table.rows.length; ++i)
	{
		Arr[i] = new Array(Table.rows[i].cells.length);
		for (j = 0; j < Table.rows[i].cells.length; ++j)
			Arr[i][j] = Table.rows[i].cells[j].innerHTML;
	}
	return Arr;
}

function SortArray(Arr, Col, Cmp)
{
	if (Arr.length == 0)
		return ;
	if (Arr[0].length <= Col)
		return ;
	SID = new Array(Arr.length);
	for (i = 0; i < Arr.length; ++i)
	{
		SID[i] = new Object();
		SID[i].id = i;
		SID[i].val = Arr[i][Col];	
	}
	SID.sort(function(a, b){
		return Cmp(a.val, b.val);
	});

	Arr_New = new Array(Arr.length);

	for (i = 0; i < Arr.length; ++i)
		Arr_New[i] = Arr[SID[i].id];
	for (i = 0; i < Arr.length; ++i)
		Arr[i] = Arr_New[i];
}

function SortTable(Table, Col, Cmp)
{
	if (Table.rows.length == 0)
		return ;
	if (Table.rows[0].cells.length <= Col)
		return ;
	SID = new Array(Table.rows.length);
	for (i = 0; i < Table.rows.length; ++i)
	{
		SID[i] = new Object();
		SID[i].id = i;
		SID[i].val = Table.rows[i].cells[Col].innerHTML;	
	}

	SID.sort(function(a, b){
		return Cmp(a.val, b.val);
	});

	Arr_New = new Array(Table.rows.length);

	for (i = 0; i < Arr_New.length; ++i)
	{
		Arr_New[i] = Table.rows[SID[i].id].valueOf();
	}

	for (i = 0; i < Table.rows.length; ++i)
		Table.rows[i] = Arr_New[i].valueOf();
}


function WriteTable(Table, Arr)
{
	for (i = 0; i < Arr.length; ++i)
	{
		for (j = 0; j < Arr[i].length; ++j)
		{
			cell = Table.rows[i].cells[j];
			cell.innerHTML = Arr[i][j];
		}
	}
}

function MakeTable(Table, Arr)
{
	len = Table.rows.length;
	for (i = 0; i < len; ++i)
		Table.deleteRow(0);
	for (i = 0; i < Arr.length; ++i)
	{
		row = Table.insertRow(i);
		for (j = 0; j < Arr[i].length; ++j)
		{
			cell = row.insertCell(j);
			cell.innerHTML = Arr[i][j];
		}
	}
}

